﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using Castle.MicroKernel.Registration;

namespace MusicStore.Silverlight.Infrastructure
{
	public class CastleWindsorServiceLocator : IServiceLocator
	{
		private IWindsorContainer Container;

		public CastleWindsorServiceLocator() : this(new WindsorContainer())
		{
		}

		public CastleWindsorServiceLocator(IWindsorContainer container)
		{
			Container = container;
			Init();
		}

		private void Init()
		{
			Container.Register(
				Component.For<IServiceLocator>().Instance(this)
			);
		}

		public void Configure(Action<object> configAction)
		{
			configAction(Container);
		}

		public T Resolve<T>()
		{
			return Container.Resolve<T>();
		}

		public T Resolve<T>(string name)
		{
			return Container.Resolve<T>(name);
		}

		public bool CanResolve(string name)
		{
			return Container.Kernel.HasComponent(name);
		}
	}
}
